package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.GoodBrand;
import com.caocaog.market.service.IGoodBrandService;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Description
 * @Author Van.Planifolia
 * @Date 2024/1/9
 * @Version 1.0
 */
@RestController
@RequestMapping("/goodBrand")
public class GoodBrandController extends JeecgController<GoodBrand, IGoodBrandService> {

    @Resource
    private IGoodBrandService goodBrandService;

    /**
     * 分页列表查询
     *
     * @param goodBrand 查询参数
     * @param pageNo    分页页码
     * @param pageSize  分页大小
     * @param req       查询过滤器的Request
     * @return 查询结果
     */
    @ApiOperation(value = "品牌-分页列表查询", notes = "品牌上报日志-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<GoodBrand>> queryPageList(GoodBrand goodBrand,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<GoodBrand> queryWrapper = QueryGenerator.initQueryWrapper(goodBrand, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<GoodBrand> page = new Page<GoodBrand>(pageNo, pageSize);
        IPage<GoodBrand> pageList = goodBrandService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 品牌-编辑
     *
     * @param goodBrand 商品编辑参数
     * @return 编辑结果
     */
    @AutoLog(value = "品牌-编辑")
    @ApiOperation(value = "品牌-编辑", notes = "设备上报日志-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody GoodBrand goodBrand) {
        goodBrandService.updateById(goodBrand);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id 被删除的品牌ID
     * @return 删除结果
     */
    @AutoLog(value = "品牌-通过id删除")
    @ApiOperation(value = "品牌-通过id删除", notes = "品牌-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        goodBrandService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids 被删除的IDs
     * @return 删除结果
     */
    @AutoLog(value = "品牌-批量删除")
    @ApiOperation(value = "设备上报日志-批量删除", notes = "设备上报日志-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        goodBrandService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id 被查询的ID
     * @return 查询结果
     */
    @ApiOperation(value = "品牌-通过id查询", notes = "设备上报日志-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        GoodBrand jnDeviceLog = goodBrandService.getById(id);
        if (jnDeviceLog == null) {
            return Result.error("未找到对应数据", null);
        }
        return Result.OK(jnDeviceLog);
    }

    /**
     * 通过userId查询
     *
     * @param userId 被查询的ID
     * @return 查询结果
     */
    @ApiOperation(value = "品牌-通过id查询", notes = "设备上报日志-通过id查询")
    @GetMapping(value = "/queryByUserId")
    public Result<?> queryByUserId(@RequestParam(name = "userId") String userId) {
        GoodBrand goodBrand = goodBrandService.getOne(new LambdaQueryWrapper<GoodBrand>().eq(GoodBrand::getUserId, userId));
        if (goodBrand == null) {
            return Result.error("未找到对应数据", null);
        }
        return Result.OK(goodBrand);
    }

}
